package lanou3g.myfirstapp36kr.activity;

import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.VolleyError;

import lanou3g.myfirstapp36kr.R;
import lanou3g.myfirstapp36kr.StringUrl;
import lanou3g.myfirstapp36kr.adapter.FragmentNewsLVAdapter;
import lanou3g.myfirstapp36kr.base.BaseActivity;
import lanou3g.myfirstapp36kr.entity.FragmentNewsLVEntity;
import lanou3g.myfirstapp36kr.entity.FragmentNewsLVEntity.DataBean.DataBean1;
import lanou3g.myfirstapp36kr.myvolleysingleton.VolleySingleton;

/**
 * Created by dllo on 16/8/12.
 */
public class Learn36krActivity extends BaseActivity {
    private ListView listView;
    private ImageView imageView;
    private FragmentNewsLVAdapter adapter;
    @Override
    protected int getLayout() {
        return R.layout.activity_learn_36kr;
    }

    @Override
    protected void initView() {
        listView = (ListView) findViewById(R.id.list_view_learn36kr);
        imageView = (ImageView) findViewById(R.id.iv_back_learn36kr);
        adapter = new FragmentNewsLVAdapter(this);
        findViewById(R.id.iv_back_learn36kr).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }

    @Override
    protected void initData() {

        VolleySingleton.addRequest(StringUrl.stringFragmentNewsLv, FragmentNewsLVEntity.class,
                new Response.Listener<FragmentNewsLVEntity>() {
                    @Override
                    public void onResponse(FragmentNewsLVEntity response) {
                        adapter.setEntity(response);
                        listView.setAdapter(adapter);
                        //ListView加点击事件跳转详情
                        listView.setOnItemClickListener(new OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                Intent intent = new Intent(Learn36krActivity.this, NewsDetailsActivity.class);
                                DataBean1 bean = (DataBean1) adapterView.getAdapter().getItem(i);
                                String pictureUrl = bean.getFeatureImg();
                                //if (BuildConfig.DEBUG) Log.d("Fragmentnews______", pictureUrl);
                                intent.putExtra("pictureUrl", pictureUrl);
                                String name = bean.getUser().getName();
                                intent.putExtra("name", name);
                                //if (BuildConfig.DEBUG) Log.d("Fragmentnews===", name);
                                String title = bean.getTitle();
                                intent.putExtra("title", title);
                                String feedId = bean.getFeedId();
                                String detailsUrl = "https://rong.36kr.com/api/mobi/news/" + feedId;
                                intent.putExtra("detailsUrl", detailsUrl);
                                long publishTime = bean.getPublishTime();
                                intent.putExtra("publishTime", publishTime);
                                startActivity(intent);

                            }
                        });


                    }
                }, new ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.getMessage();

                    }
                });

    }
}
